package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ct_start
 * @since 2021-08-07
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    // 把 service注入


    // 这个类里面已经实现 ServiceImpl 且继承了 BaseMapper 所有可以直接在Controller层操控获得数据
     @Autowired
    EduTeacherService eduTeacherService;


    // 查询讲师表中的所有数据
    // Rest风格
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);

        return  R.ok().data("items",list);
    }

    @ApiOperation(value = "删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id){
        R flag = eduTeacherService.removeById(id) ? R.ok() : R.error();

         return flag;
    }

    // 分页查询的方法  当前页就是想返回分页完之后你所在的第几页
    @ApiOperation(value = "分页查询所有讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        // 创建page 对象
        Page<EduTeacher> page=new Page<>(current,limit);

        eduTeacherService.page(page,null);

        long total = page.getTotal(); // 总数据
        List<EduTeacher> records = page.getRecords(); // 每页数据的集合

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);

        return  R.ok().data(map);
    }

    // 条件查询代分页的方法
    @ApiOperation(value = "分页讲师列表")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery){

        // 创建page对象
        Page<EduTeacher> page=new Page<>(current,limit);
        // 构建条件
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();


        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if (level!=null){
            wrapper.eq("level",level);
        }

        // 大于等于
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        // 小于等于
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_end",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");


        //调用wrapper 实现分页功能
        eduTeacherService.page(page,wrapper);
        // 多条件查询  拼接动态sql
        // 判断条件值是否为空,如果不为空则拼接


        // 把分页的信息进行json返回
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records); //返回每页的记录数
        return  R.ok().data(map);
    }


    // 添加讲师的接口方法
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody(required = false) EduTeacher eduTeacher){

        R r=eduTeacherService.save(eduTeacher)?R.ok():R.error();

        return  r;
    }

    // 根据讲师ID 进行查询
    @ApiOperation(value = "根据Id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable String id){

//        try {
//            int i=10;
//            i=i/0;
//        } catch (Exception e) {
//          throw   new GuliException(500,"除0异常");
//        }


        EduTeacher teacher = eduTeacherService.getById(id);
        boolean flag=(teacher!=null)?true:false;

        if (flag){
            return R.ok().data("teacher",teacher);
        }
        else {
            return R.error().message("没有此讲师,请重新输入讲师ID");
        }

    }
     // 讲师修改功能
     @ApiOperation(value = "讲师修改功能")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
         boolean b = eduTeacherService.updateById(teacher);

         if (b){
            return R.ok().message("修改成功");
        }
        else {
            return R.error().message("修改失败");
        }
    }


}

